package com.lonely.my.rmi.consumer;

import com.lonely.my.rmi.UserInterface;
import com.lonely.my.rmi.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.nio.charset.StandardCharsets;
import java.rmi.Naming;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @date: 2022-03-23 17:44
 * @author: ztkj-hzb
 * @description:
 **/
@Slf4j
public class Consumer {


    public static void main(String[] args) throws Exception {

        //通过服务名获取对应的请求地址
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper("101.132.184.56:12181", 3000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                log.info("连接成功");
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();


        //获取请求地址
        byte[] data = zooKeeper.getData("/myRmi/userInterface", false, new Stat());

        UserInterface userInterface = (UserInterface) Naming.lookup(new String(data, StandardCharsets.UTF_8));
        List<User> userList = userInterface.getUserList();
        for (User user : userList) {
            System.out.println(user.toString());
        }

    }


}
